﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;

namespace game
{

    public class TrackingSystem
    {
        private Scene scene;
        private Ship playerShip;

        private Texture2D enemyMarker;
        private Texture2D allyMarker;
        private Texture2D rocketMarker;

        private SpriteBatch spriteBatch;

        private float scrWidth;
        private float scrHeight;

        public TrackingSystem(Scene scene, Ship playerShip)
        {
            this.scene = scene;
            this.playerShip = playerShip;

            this.spriteBatch = ShooterGame.Instance.SpriteBatch;

            this.enemyMarker = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\celownik3");
            this.allyMarker = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\celownik2");
            this.rocketMarker = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\celownik4");

            this.scrWidth = ShooterGame.Instance.GraphicsDevice.PresentationParameters.BackBufferWidth;
            this.scrHeight = ShooterGame.Instance.GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        public void Update(GameTime gameTime)
        {
        }

        public void Draw(GameTime gameTime)
        {
            Rectangle destRect = new Rectangle();

            spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None);

            int playerArmyID = playerShip.Army.ID;
            Vector3 playerPos = playerShip.Position;

            Texture2D marker = null;

            Matrix camMat = scene.ActiveCamera.View * scene.ActiveCamera.Projection;

            foreach(GameObject obj in scene)
            {
                if (obj is Ship && obj != playerShip)
                {
                    Vector3 objPos = obj.Position;

                    Vector3 fromPlayerToObj;
                    float dist;
                    Vector3.Subtract(ref objPos, ref playerPos, out fromPlayerToObj);

                    dist = fromPlayerToObj.Length();

                    if (dist < 200000)
                    {

                        int armyID = (obj as Ship).Army.ID;
                        if (armyID == playerArmyID)
                        {
                            marker = allyMarker;
                        }
                        else
                        {
                            marker = enemyMarker;
                        }

                        Vector4 objPosEx = new Vector4(objPos, 1.0f);

                        Vector4.Transform(ref objPosEx, ref camMat, out objPosEx);

                        float wInv = 1.0f / objPosEx.W;

                        float objPosClipSpaceX = objPosEx.X * wInv;
                        float objPosClipSpaceY = -objPosEx.Y * wInv;

                        float z = objPosEx.Z * wInv;

                        if (objPosClipSpaceX > -1.2f && objPosClipSpaceX < 1.2f && objPosClipSpaceY > -1.2f && objPosClipSpaceY < 1.2f && z >= 0.0f && z <= 1.0f)
                        {

                            int halfSize = (int)(4000000.0f / dist);

                            destRect.X = (int)((objPosClipSpaceX * 0.5f + 0.5f) * scrWidth) - halfSize;
                            destRect.Y = (int)((objPosClipSpaceY * 0.5f + 0.5f) * scrHeight) - halfSize;

                            destRect.Width = halfSize * 2;
                            destRect.Height = halfSize * 2;

                            spriteBatch.Draw(marker, destRect, Color.White);

                            if (obj == playerShip.Target)
                            {
                                spriteBatch.Draw(rocketMarker, destRect, Color.White);//, gameTime.ElapsedGameTime.Milliseconds, new Vector2(0,0), SpriteEffects.None, 0);
                            }
                        }
                    

                    }
                }
            }

           


            spriteBatch.End();
        }
    }
}
